<template>
  <el-dialog
    title="行程轨迹"
    :visible.sync="centerDialogVisibleOn"
    :before-close="onClose"
    width="90%"
    center
  >
    <baidu-map
      class="bm-view"
      ak="C8y5QNyQ2N3Lk8ufQh71iH383YrC1ZPK"
      :center="center"
      :zoom="zoom"
      @ready="handler"
      scroll-wheel-zoom
      ref="map"
    >
      <bm-marker
        v-for="(item,index) in dataList" :key="index+'marker'"
        :position="{lng:item.lng,lat:item.lat}"
        @click="infoWindowOpen(index)"
      >
        <bm-info-window
          :closeOnClick="false"
          :show="item.show"
          :title="'用户：'+item.userName"
          @open="infoWindowOpen(index)"
          @close="infoWindowClose(index)"
        >
          <div style="display: flex;flex-direction: column;">
            <div>坐标：{{ item.location }}</div>
            <div>地址：{{ item.address }}</div>
            <div>拜访开始时间：{{ item.signInTime }}</div>
            <div>拜访结束时间：{{ item.signOutTime }}</div>
            <div>拜访时长分钟：{{ item.visitDuration }}</div>
            <div>备注：{{ item.remark }}</div>
          </div>
        </bm-info-window>
      </bm-marker>
    </baidu-map>
    <el-form size="small" label-width="70px" class="map_form">
      <el-row :gutter="10" class="map_row">
        <el-col :span="24">
          <el-form-item label="日期">
            <el-date-picker
              v-model="query.date"
              type="date"
              size="small"
              class="date-item"
              style="width: 100%;"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="业务员">
            <zSelect
              :value.sync="query.userIds"
              url="api/dropDown/sysUserList"
              @getChange="getInfo"
              style="width:  100%;"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="部门">
            <TreeSelect
              v-model="query.deptIds"
              :options="depts"
              placeholder="选择部门"
              style="width:  100%"
              :normalizer="normalizer"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import TreeSelect from '@riophae/vue-treeselect'
import { getDepts } from '@/api/system/dept'
import { queryDetail } from '@/api/erp/reportSalesmanTrace'
import { parseDate } from '@/utils'

export default {
  name: 'crm-cus-visit-map',
  data() {
    return {
      query: {
        date: this.date,
        deptIds: null,
        userIds: this.userId
      },
      center: { lng: 0, lat: 0 },
      dataList: [],
      depts: [],
      zoom: 15,
      centerDialogVisibleOn: false
    }
  },
  components: { TreeSelect },
  props: ['userId', 'centerDialogVisible', 'date'],
  watch: {
    query: {
      handler(val) {
        this.onLists()
      },
      deep: true
    }
  },
  created() {
    this.getDepts()
    this.$nextTick(() => {
      this.centerDialogVisibleOn = this.centerDialogVisible
    })
  },
  methods: {
    // 初始化地图
    handler({ BMap, map }) {
      this.onLists()
    },
    getInfo(e) {
      this.query.userIds = e.id
      this.onLists()
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || (node.children && node.children.length == 0)) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    infoWindowClose(index) {
      this.dataList[index].show = false
    },
    infoWindowOpen(index) {
      this.dataList[index].show = true
    },
    getDepts() {//部门
      getDepts({ status: 1 }).then(res => {
        this.depts = res
      })
    },
    onLists() {//详情
      const [year, month, day] = parseDate(this.query.date, '{y}-{m}-{d}').split('-')
      this.query.year = year
      this.query.month = month
      this.query.day = day
      queryDetail(this.query).then((res) => {
        if (res.status === 200) {
          this.dataList = res.data.map((item) => {
            const [lng, lat] = item.location.split(',')
            item.lng = lng
            item.lat = lat
            item.show = false
            return item
          })
          if (this.dataList.length) {
            this.center.lng = this.dataList[0].lng || 116.404
            this.center.lat = this.dataList[0].lat || 39.915
            this.zoom = 6
            this.dataList[0].show = true
          }
        } else {
          this.dataList = []
        }
      })
    },
    onClose() {
      this.$emit('onCenterDialogVisible')
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {
  .el-dialog--center .el-dialog__body {
    padding: 0 25px 20px;
  }
}

.map_form {
  position: absolute;
  z-index: 2;
  right: 0;
  top: 0;
  background-color: #eee;
  padding: 20px 20px 20px 0;
  height: 100%;

  .map_row {
    display: flex;
    flex-direction: column;
    width: 250px;
  }
}

.bm-view {
  width: 100%;
  height: 600px;
  position: relative;
}
</style>
